package com.ruix.app.ui.main;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ruix.app.R;
import com.ruix.app.RuiApplication;
import com.ruix.app.cache.CacheFactory;
import com.ruix.app.constant.RuiXConstant;
import com.ruix.app.manager.UserManager;
import com.ruix.app.mapper.SystemConfigMapper;
import com.ruix.app.mapper.UserMapper;
import com.ruix.app.model.AppUpgradeConfig;
import com.ruix.app.model.SystemConfig;
import com.ruix.app.model.User;
import com.ruix.app.model.WebParcel;
import com.ruix.app.network.ApiConstant;
import com.ruix.app.network.RXObserver;
import com.ruix.app.network.RequestFactory;
import com.ruix.app.ui.CommonWebActivity;
import com.ruix.app.ui.base.BaseFragment;
import com.ruix.app.ui.usercenter.BuyRecordActivity;
import com.ruix.app.ui.usercenter.PurseActivity;
import com.ruix.app.ui.usercenter.SettingActivity;
import com.ruix.app.ui.usercenter.UserConfigActivity;
import com.ruix.app.util.RoleUtil;

import io.reactivex.disposables.Disposable;

/**
 * Created by hadexs on 2017/4/10.
 */

public class UserCenterFragment extends BaseFragment implements View.OnClickListener{

    private TextView userNameView;
    SimpleDraweeView userAvatar;
    private User user;
    private View pusrseView;

    private BroadcastReceiver myReceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent) {
            if (RuiXConstant.NOTICE_CHANGE_USER_INFO.equals(intent.getAction())){
                updateView();
            }
        }

    };


    @Override
    protected int loadViewLayout() {
        return R.layout.fragment_user_center;
    }


    @Override
    protected void initView() {
         userAvatar = (SimpleDraweeView) mFragmentView.findViewById(R.id.iv_user_avatar);
         userNameView = (TextView) mFragmentView.findViewById(R.id.tv_user_name);
         pusrseView =  mFragmentView.findViewById(R.id.rl_user_center_purse);
    }

    @Override
    protected void initLocalData() {
        updateView();
    }

    private void updateView(){
        user =UserManager.getCurrentUser();
        if (user != null) {
            userNameView.setText(user.getName());
            userAvatar.setImageURI(user.getAvatar());

            if (user.getGender() == 1) {
                Drawable drawable = getActivity().getResources().getDrawable(R.drawable.auther_gender_male);
                drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                userNameView.setCompoundDrawables(null, null, drawable, null);
            } else {
                Drawable drawable = getActivity().getResources().getDrawable(R.drawable.auther_gender_female);
                drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                userNameView.setCompoundDrawables(null, null, drawable, null);
            }
        }


        if (user.hasPurse()){
            pusrseView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initListener() {
        mFragmentView.findViewById(R.id.rl_user_center_purse).setOnClickListener(this);
        mFragmentView.findViewById(R.id.rl_user_center_purchase).setOnClickListener(this);
        mFragmentView.findViewById(R.id.rl_user_center_setting).setOnClickListener(this);
        mFragmentView.findViewById(R.id.rl_user_center_about).setOnClickListener(this);
        mFragmentView.findViewById(R.id.ll_user_center_userinfo).setOnClickListener(this);
        registerReceivers();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_user_center_purse:
                Intent intent1 = new Intent(getActivity(), PurseActivity.class);
                getActivity().startActivity(intent1);
                break;
            case R.id.rl_user_center_purchase:

                Intent intent2 = new Intent(getActivity(), BuyRecordActivity.class);
                getActivity().startActivity(intent2);
                break;
            case R.id.rl_user_center_setting:

                Intent intent3 = new Intent(getActivity(), SettingActivity.class);
                getActivity().startActivity(intent3);
                break;
            case R.id.rl_user_center_about:
                loadAbout();
//                Intent intent4 = new Intent(getActivity(), AboutRuixActivity.class);
//                getActivity().startActivity(intent4);
                break;
            case R.id.ll_user_center_userinfo:
                Intent intent5 = new Intent(getActivity(), UserConfigActivity.class);
                getActivity().startActivity(intent5);
                break;


        }
    }

    @Override
    public void onResume() {
        super.onResume();
        laodSystemInfo();
    }

    private void laodSystemInfo() {
        RequestFactory.systemInfo(new SystemConfigMapper(), new RXObserver<SystemConfig>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SystemConfig value) {
                if (value != null) {
                    updateView();
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {

            }
        });
    }


    private void registerReceivers(){
        IntentFilter filter = new IntentFilter();
        filter.addAction(RuiXConstant.NOTICE_CHANGE_USER_INFO);
        getActivity().registerReceiver(myReceiver, filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceivers();
    }

    private void unregisterReceivers(){
        if (myReceiver != null){
            getActivity().unregisterReceiver(myReceiver);
        }
    }



    private void loadAbout(){
        WebParcel parcel = new WebParcel();
        parcel.setTitle("关于锐学");
        String abouturl = ApiConstant.App_About+ RuiApplication.appVersionName;
        parcel.setUrl(abouturl);
        startActivity(CommonWebActivity.newIntent(this.getActivity(), parcel));
    }


}
